<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.0.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.0.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.0.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.0.4">


  <link rel="mask-icon" href="/images/logo.svg?v=6.0.4" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '6.0.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  
  <meta name="keywords" content="技术博客,php,java,python,go" />


<meta name="description" content="接口设计ECShop接口使用了Lumen框架开发。所有接口不论错误还是正确都返回了200，接口业务实现在 Model 类，业务方法均定义为 static。接口大部分都设置为POST请求，只有少数几个为GET请求。 接口公共参数X-ECAPI-UserAgent   如：Platform/ios, Browser/chromeX-ECAPI-Ver    如：3.5.0 登录接口">
<meta property="og:type" content="article">
<meta property="og:title" content="ECShop开源系统代码学习笔记">
<meta property="og:url" content="http://blog.yunshu168.com/posts/b727584f.html">
<meta property="og:site_name" content="树下千云">
<meta property="og:description" content="接口设计ECShop接口使用了Lumen框架开发。所有接口不论错误还是正确都返回了200，接口业务实现在 Model 类，业务方法均定义为 static。接口大部分都设置为POST请求，只有少数几个为GET请求。 接口公共参数X-ECAPI-UserAgent   如：Platform/ios, Browser/chromeX-ECAPI-Ver    如：3.5.0 登录接口">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/006tNbRwgy1g9lxsapk41j31ec0p4gpw.jpg">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/006tNbRwgy1g9ly2wirrbj31eq0mktbq.jpg">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/006tNbRwgy1g9ly4k25iwj31bs0u0n1i.jpg">
<meta property="og:updated_time" content="2020-03-10T07:09:08.956Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="ECShop开源系统代码学习笔记">
<meta name="twitter:description" content="接口设计ECShop接口使用了Lumen框架开发。所有接口不论错误还是正确都返回了200，接口业务实现在 Model 类，业务方法均定义为 static。接口大部分都设置为POST请求，只有少数几个为GET请求。 接口公共参数X-ECAPI-UserAgent   如：Platform/ios, Browser/chromeX-ECAPI-Ver    如：3.5.0 登录接口">
<meta name="twitter:image" content="https://tva1.sinaimg.cn/large/006tNbRwgy1g9lxsapk41j31ec0p4gpw.jpg">






  <link rel="canonical" href="http://blog.yunshu168.com/posts/b727584f.html"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>
  <title>ECShop开源系统代码学习笔记 | 树下千云</title>
  






  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?f8a99180bb75f49a4b2206cab6e25c50";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <!-- <div class="headband"></div>
<a href="https://github.com/yunshu2009"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a> -->
    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">树下千云</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />搜索</a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>


  



 </div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://blog.yunshu168.com/posts/b727584f.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="云舒">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="树下千云">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">ECShop开源系统代码学习笔记</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <!--<span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>-->
              
                <!--<span class="post-meta-item-text">发表于</span>-->
              
              <!--<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-11-05T16:37:09+08:00">2019-11-05</time>-->
            

            
            

            
          </span>

          

          
            
          

          
          

          


          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="接口设计"><a href="#接口设计" class="headerlink" title="接口设计"></a>接口设计</h3><p>ECShop接口使用了Lumen框架开发。所有接口不论错误还是正确都返回了200，接口业务实现在 Model 类，业务方法均定义为 static。接口大部分都设置为POST请求，只有少数几个为GET请求。</p>
<p>接口公共参数<br>X-ECAPI-UserAgent   如：Platform/ios, Browser/chrome<br>X-ECAPI-Ver    如：3.5.0</p>
<h4 id="登录接口"><a href="#登录接口" class="headerlink" title="登录接口"></a>登录接口</h4><p><img src="https://tva1.sinaimg.cn/large/006tNbRwgy1g9lxsapk41j31ec0p4gpw.jpg" alt=""></p>
<a id="more"></a>
<p>token为JWT，payload中包含了uid。,如 <code>{
  &quot;uid&quot;: 3,
  &quot;exp&quot;: 1578127605,
  &quot;platform&quot;: &quot;ios&quot;
}</code> JWT实现在：api目录/helper/Token.php</p>
<p>路由需要经过xss中间件(对所有的参数递归使用了strip_tags去掉html标签）。</p>
<p>递归函数 array-walk-recursive：<br><a href="https://www.runoob.com/php/func-array-walk-recursive.html" target="_blank" rel="noopener">https://www.runoob.com/php/func-array-walk-recursive.html</a></p>
<h4 id="需要认证的接口"><a href="#需要认证的接口" class="headerlink" title="需要认证的接口"></a>需要认证的接口</h4><p>举例：获取用户接口</p>
<p><img src="https://tva1.sinaimg.cn/large/006tNbRwgy1g9ly2wirrbj31eq0mktbq.jpg" alt=""></p>
<p>需要传递 <code>X-ECAPI-Authorization</code> 这个header。路由经过token、xss中间件。</p>
<h4 id="匿名访问接口"><a href="#匿名访问接口" class="headerlink" title="匿名访问接口"></a>匿名访问接口</h4><p><img src="https://tva1.sinaimg.cn/large/006tNbRwgy1g9ly4k25iwj31bs0u0n1i.jpg" alt=""></p>
<p>路由需要经过xss中间件。</p>
<p>xss中间件：<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Middleware</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">Closure</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">XSSProtection</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Handle an incoming request.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>  \Illuminate\Http\Request  $request</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>  \Closure  $next</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">handle</span><span class="params">($request, Closure $next)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!in_array(strtolower($request-&gt;method()), [<span class="string">'put'</span>, <span class="string">'post'</span>])) &#123;</span><br><span class="line">            <span class="keyword">return</span> $next($request);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $input = $request-&gt;all();</span><br><span class="line"></span><br><span class="line">        array_walk_recursive($input, <span class="function"><span class="keyword">function</span> <span class="params">(&amp;$input)</span> </span>&#123;</span><br><span class="line">            $input = strip_tags($input);</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        $request-&gt;merge($input);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $next($request);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>token中间件：<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Middleware</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">Closure</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Http</span>\<span class="title">Response</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Helper</span>\<span class="title">Token</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Helper</span>\<span class="title">Protocol</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TokenAuthenticate</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Handle an incoming request.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>  \Illuminate\Http\Request  $request</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>  \Closure  $next</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">handle</span><span class="params">($request, Closure $next)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $token = Token::authorization();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($token === <span class="keyword">false</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> show_error(<span class="number">10001</span>, trans(<span class="string">'message.token.invalid'</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($token ===  <span class="string">'token-expired'</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> show_error(<span class="number">10002</span>, trans(<span class="string">'message.token.expired'</span>));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $next($request);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="接口实现"><a href="#接口实现" class="headerlink" title="接口实现"></a>接口实现</h3><h4 id="Model-基类"><a href="#Model-基类" class="headerlink" title="Model 基类"></a>Model 基类</h4><p>BaseModel 类定义了错误码和返回正确响应的formatBody、formatPaged方法以及返回错误响应的formatError方法。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span> </span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Models</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Database</span>\<span class="title">Eloquent</span>\<span class="title">Model</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BaseModel</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">const</span> SUCCESS         = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">const</span> UNKNOWN_ERROR   = <span class="number">10000</span>;</span><br><span class="line">    <span class="keyword">const</span> INVALID_SESSION = <span class="number">10001</span>;</span><br><span class="line">    <span class="keyword">const</span> EXPIRED_SESSION = <span class="number">10002</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> BAD_REQUEST     = <span class="number">400</span>;</span><br><span class="line">    <span class="keyword">const</span> UNAUTHORIZED    = <span class="number">401</span>;</span><br><span class="line">    <span class="keyword">const</span> NOT_FOUND       = <span class="number">404</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 平台类型</span></span><br><span class="line">    <span class="keyword">const</span> B2C             = <span class="number">0</span>;    <span class="comment">//单店</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 平台厂商</span></span><br><span class="line">    <span class="keyword">const</span> ECSHOP          = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">formatPaged</span><span class="params">($page, $size, $total)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> [</span><br><span class="line">            <span class="string">'total'</span> =&gt; $total,</span><br><span class="line">            <span class="string">'page'</span> =&gt; $page,</span><br><span class="line">            <span class="string">'size'</span> =&gt; $size,</span><br><span class="line">            <span class="string">'more'</span> =&gt; ($total &gt; $page * $size) ? <span class="number">1</span> : <span class="number">0</span></span><br><span class="line">        ];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">formatBody</span><span class="params">(array $data = [])</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $data[<span class="string">'error_code'</span>] = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">return</span> $data;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">formatError</span><span class="params">($code, $message = null)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">switch</span> ($code) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="keyword">self</span>::UNKNOWN_ERROR:</span><br><span class="line">                $message = trans(<span class="string">'message.error.unknown'</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            </span><br><span class="line">            <span class="keyword">case</span> <span class="keyword">self</span>::NOT_FOUND:</span><br><span class="line">                $message = trans(<span class="string">'message.error.404'</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $body[<span class="string">'error'</span>] = <span class="keyword">true</span>;</span><br><span class="line">        $body[<span class="string">'error_code'</span>] = $code;</span><br><span class="line">        $body[<span class="string">'error_desc'</span>] = $message;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $body;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="参数校验和返回json结果"><a href="#参数校验和返回json结果" class="headerlink" title="参数校验和返回json结果"></a>参数校验和返回json结果</h4><p>控制器</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>\<span class="title">v2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>\<span class="title">Controller</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>\<span class="title">Member</span>;</span><br><span class="line">.</span><br><span class="line">.</span><br><span class="line">.</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">		 <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * POST /user/signin</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">signin</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $rules = [</span><br><span class="line">            <span class="string">'username'</span> =&gt; <span class="string">'required|string'</span>,</span><br><span class="line">            <span class="string">'password'</span> =&gt; <span class="string">'required|min:6|max:20'</span></span><br><span class="line">        ];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($error = <span class="keyword">$this</span>-&gt;validateInput($rules)) &#123;</span><br><span class="line">            <span class="keyword">return</span> $error;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $data = Member::login(<span class="keyword">$this</span>-&gt;validated);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">$this</span>-&gt;json($data);</span><br><span class="line">    &#125;</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>调用控制器父类的 validateInput 方法，最后再调用 json 方法返回数据</p>
<p>Controller类：<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">Laravel</span>\<span class="title">Lumen</span>\<span class="title">Routing</span>\<span class="title">Controller</span> <span class="title">as</span> <span class="title">BaseController</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">Validator</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">Log</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Helper</span>\<span class="title">Token</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">BaseModel</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Controller</span> <span class="keyword">extends</span> <span class="title">BaseController</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> $validated;</span><br><span class="line">    <span class="keyword">public</span> $request;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">$this</span>-&gt;request = app(<span class="string">'request'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 验证输入信息</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>  array $rules</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> response</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">validateInput</span><span class="params">($rules)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $requests = <span class="keyword">$this</span>-&gt;request-&gt;all();</span><br><span class="line"></span><br><span class="line">        $validator = Validator::make($requests, $rules);</span><br><span class="line">        <span class="keyword">if</span> ($validator-&gt;fails()) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">self</span>::json(BaseModel::formatError(BaseModel::BAD_REQUEST, $validator-&gt;messages()-&gt;first()));</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">$this</span>-&gt;validated = array_intersect_key($requests, $rules);</span><br><span class="line">            <span class="keyword">$this</span>-&gt;validated = $requests ;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 自定义验证</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">customValidate</span><span class="params">($requests, $rules)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $validator = Validator::make($requests, $rules);</span><br><span class="line">        <span class="keyword">if</span> ($validator-&gt;fails()) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">self</span>::json(BaseModel::formatError(BaseModel::BAD_REQUEST, $validator-&gt;messages()-&gt;first()));</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 返回Json数据</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>  array   $data</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>  array   $ext</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>  array   $paged</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> json</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">json</span><span class="params">($body = false)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">//过滤null为空字符串(需协调客户端兼容)</span></span><br><span class="line">        <span class="comment">// if ($body) &#123;</span></span><br><span class="line">        <span class="comment">//     $body = format_array($body);</span></span><br><span class="line">        <span class="comment">// &#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 写入日志</span></span><br><span class="line">        <span class="keyword">if</span> (config(<span class="string">'app.debug'</span>)) &#123;</span><br><span class="line">            $debug_id = uniqid();</span><br><span class="line"></span><br><span class="line">            Log::debug($debug_id, [</span><br><span class="line">                <span class="string">'LOG_ID'</span>         =&gt; $debug_id,</span><br><span class="line">                <span class="string">'IP_ADDRESS'</span>     =&gt; <span class="keyword">$this</span>-&gt;request-&gt;ip(),</span><br><span class="line">                <span class="string">'REQUEST_URL'</span>    =&gt; <span class="keyword">$this</span>-&gt;request-&gt;fullUrl(),</span><br><span class="line">                <span class="string">'AUTHORIZATION'</span>  =&gt; <span class="keyword">$this</span>-&gt;request-&gt;header(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-Authorization'</span>),</span><br><span class="line">                <span class="string">'REQUEST_METHOD'</span> =&gt; <span class="keyword">$this</span>-&gt;request-&gt;method(),</span><br><span class="line">                <span class="string">'PARAMETERS'</span>     =&gt; <span class="keyword">$this</span>-&gt;validated,</span><br><span class="line">                <span class="string">'RESPONSES'</span>      =&gt; $body</span><br><span class="line">            ]);</span><br><span class="line"></span><br><span class="line">            $body[<span class="string">'debug_id'</span>] = $debug_id;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">isset</span>($body[<span class="string">'error'</span>]) &amp;&amp; $body[<span class="string">'error'</span>]) &#123;</span><br><span class="line">            <span class="keyword">unset</span>($body[<span class="string">'error'</span>]);</span><br><span class="line">            $response = response()-&gt;json($body);</span><br><span class="line">            $response-&gt;header(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-ErrorCode'</span>, $body[<span class="string">'error_code'</span>]);</span><br><span class="line">            $response-&gt;header(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-ErrorDesc'</span>, urlencode($body[<span class="string">'error_desc'</span>]));</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $response = response()-&gt;json($body);</span><br><span class="line">            $response-&gt;header(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-ErrorCode'</span>, <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (config(<span class="string">'token.refresh'</span>)) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($new_token = Token::refresh()) &#123;</span><br><span class="line">                <span class="comment">// 生成新token</span></span><br><span class="line">                $response-&gt;header(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-New-Authorization'</span>, $new_token);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $response;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>Token类部分实现<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Helper</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> \<span class="title">DomainException</span>;</span><br><span class="line"><span class="keyword">use</span> \<span class="title">InvalidArgumentException</span>;</span><br><span class="line"><span class="keyword">use</span> \<span class="title">UnexpectedValueException</span>;</span><br><span class="line"><span class="keyword">use</span> \<span class="title">DateTime</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">Cache</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Helper</span>\<span class="title">Header</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">Log</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Token</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">		.</span><br><span class="line">		.</span><br><span class="line">		.</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">authorization</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $token = app(<span class="string">'request'</span>)-&gt;header(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-Authorization'</span>);</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">empty</span>($token)) &#123;</span><br><span class="line">            $token = app(<span class="string">'request'</span>)-&gt;get(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-Authorization'</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Log::debug('Authorization', ['token' =&gt; $token]);</span></span><br><span class="line">        <span class="keyword">if</span> ($payload = <span class="keyword">self</span>::decode($token)) &#123;</span><br><span class="line">            <span class="comment">// Log::debug('payload', ['payload' =&gt; $payload]);</span></span><br><span class="line">            <span class="keyword">if</span> (is_object($payload) &amp;&amp; property_exists($payload, <span class="string">'uid'</span>)) &#123;</span><br><span class="line">                <span class="keyword">return</span> $payload-&gt;uid;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($payload == <span class="number">10002</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">'token-expired'</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">refresh</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $token = app(<span class="string">'request'</span>)-&gt;header(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-Authorization'</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($token) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($payload = <span class="keyword">self</span>::decode($token)) &#123;</span><br><span class="line">                <span class="keyword">if</span> (is_object($payload)) &#123;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">// 超过1天</span></span><br><span class="line">                    <span class="keyword">if</span> (property_exists($payload, <span class="string">'exp'</span>)) &#123;</span><br><span class="line">                        <span class="keyword">if</span> ((time()+config(<span class="string">'token.ttl'</span>)*<span class="number">60</span>-$payload-&gt;exp) &gt; config(<span class="string">'token.refresh_ttl'</span>)*<span class="number">60</span>) &#123;</span><br><span class="line">                            <span class="keyword">return</span> <span class="keyword">self</span>::new_token($payload);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">// 版本号不匹配</span></span><br><span class="line">                    <span class="keyword">if</span> (property_exists($payload, <span class="string">'ver'</span>)) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (version_compare(config(<span class="string">'token.ver'</span>), $payload-&gt;ver) != <span class="number">0</span>) &#123;</span><br><span class="line">                            <span class="keyword">return</span> <span class="keyword">self</span>::new_token($payload);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    <span class="comment">// 没有版本号</span></span><br><span class="line">                    <span class="keyword">if</span> (!property_exists($payload, <span class="string">'ver'</span>)) &#123;</span><br><span class="line">                        <span class="keyword">return</span> <span class="keyword">self</span>::new_token($payload);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">new_token</span><span class="params">($payload)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::encode([</span><br><span class="line">                <span class="string">'uid'</span> =&gt; $payload-&gt;uid,</span><br><span class="line">                <span class="string">'ver'</span> =&gt; config(<span class="string">'token.ver'</span>)</span><br><span class="line">            ]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>在token超过1天、版本号不匹配、没有版本头的情况下会重置token，见<code>refresh()</code></p>
<h4 id="功能开启检查"><a href="#功能开启检查" class="headerlink" title="功能开启检查"></a>功能开启检查</h4><p>使用 Features::check() 检查一些功能是否开启。</p>
<p>控制器方法：<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>\<span class="title">v2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>\<span class="title">Controller</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>\<span class="title">Member</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>\<span class="title">RegFields</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>\<span class="title">Features</span>;</span><br><span class="line"></span><br><span class="line">.</span><br><span class="line">.</span><br><span class="line">.</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * POST /user/signup-email</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">signupByEmail</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $rules = [</span><br><span class="line">            <span class="string">'device_id'</span>     =&gt; <span class="string">'string'</span>,</span><br><span class="line">            <span class="string">'username'</span>      =&gt; <span class="string">'required|min:3|max:25|alpha_num'</span>,</span><br><span class="line">            <span class="string">'email'</span>         =&gt; <span class="string">'required|email'</span>,</span><br><span class="line">            <span class="string">'password'</span>      =&gt; <span class="string">'required|min:6|max:20'</span>,</span><br><span class="line">            <span class="string">'invite_code'</span>   =&gt; <span class="string">'integer'</span></span><br><span class="line">        ];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($res = Features::check(<span class="string">'signup.default'</span>)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">$this</span>-&gt;json($res);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($error = <span class="keyword">$this</span>-&gt;validateInput($rules)) &#123;</span><br><span class="line">            <span class="keyword">return</span> $error;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $data = Member::createMember(<span class="keyword">$this</span>-&gt;validated);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">$this</span>-&gt;json($data);</span><br><span class="line">    &#125;</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>Features类：<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">BaseModel</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Features</span> <span class="keyword">extends</span> <span class="title">BaseModel</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $features = [</span><br><span class="line">        <span class="string">"invoice"</span>          =&gt; <span class="keyword">true</span>,  <span class="comment">// 是否支持发票</span></span><br><span class="line">        <span class="string">"cashgift"</span>         =&gt; <span class="keyword">true</span>,  <span class="comment">// 是否支持红包</span></span><br><span class="line">        <span class="string">"score"</span>            =&gt; <span class="keyword">true</span>,  <span class="comment">// 是否支持积分</span></span><br><span class="line">        <span class="string">"coupon"</span>           =&gt; <span class="keyword">false</span>,  <span class="comment">// 是否支持优惠券</span></span><br><span class="line">        <span class="string">"address.default"</span>  =&gt; <span class="keyword">true</span>,   <span class="comment">// 是否支持默认地址</span></span><br><span class="line">        <span class="string">'signin.qq'</span>        =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'signin.weibo'</span>     =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'signin.weixin'</span>    =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'signin.wxa'</span>       =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'signin.mobile'</span>    =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'signin.default'</span>   =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'share.qq'</span>         =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'share.weibo'</span>      =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'share.weixin'</span>     =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'pay.alipay'</span>       =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'pay.weixin'</span>       =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'pay.unionpay'</span>     =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'pay.wxweb'</span>        =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'pay.alipaywap'</span>    =&gt; <span class="keyword">true</span>, <span class="comment">// 支付宝手机网站支付</span></span><br><span class="line">        <span class="string">'pay.wxa'</span>          =&gt; <span class="keyword">true</span>, <span class="comment">//小程序支付</span></span><br><span class="line">        <span class="string">'balance'</span>          =&gt; <span class="keyword">true</span>, <span class="comment">//余额支付</span></span><br><span class="line">        <span class="string">'signup.mobile'</span>    =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'signup.default'</span>   =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'findpass.mobile'</span>  =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'findpass.default'</span> =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'logistics'</span>        =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'push'</span>             =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'qiniu'</span>            =&gt; <span class="keyword">true</span>,</span><br><span class="line">        <span class="string">'statistics'</span>       =&gt; <span class="keyword">true</span>,</span><br><span class="line">    ];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getList</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::formatFeature(<span class="keyword">self</span>::$features);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">check</span><span class="params">($code)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">self</span>::getStatus($code) === <span class="keyword">false</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">self</span>::formatError(<span class="keyword">self</span>::UNAUTHORIZED, trans(<span class="string">'message.error.unauthorized'</span>));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getStatus</span><span class="params">($code)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $arr = <span class="keyword">self</span>::formatFeature(<span class="keyword">self</span>::$features);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">isset</span>($arr[$code]) ? $arr[$code] : <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">formatFeature</span><span class="params">($data)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $data;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="参数校验规则"><a href="#参数校验规则" class="headerlink" title="参数校验规则"></a>参数校验规则</h4><p>验证数字digits:value，例如：<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">$rules = [</span><br><span class="line">    <span class="string">'device_id'</span>     =&gt; <span class="string">'string'</span>,</span><br><span class="line">    <span class="string">'password'</span>      =&gt; <span class="string">'required|min:6|max:20'</span>,</span><br><span class="line">    <span class="string">'mobile'</span>        =&gt; <span class="string">'required|string'</span>,</span><br><span class="line">    <span class="string">'code'</span>          =&gt; <span class="string">'required|string|digits:6'</span>,</span><br><span class="line">    <span class="string">'invite_code'</span>   =&gt; <span class="string">'integer'</span>,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> ($error = <span class="keyword">$this</span>-&gt;validateInput($rules)) &#123;</span><br><span class="line">    <span class="keyword">return</span> $error;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>验证字母和数字 alpha_num:value，例如：<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">$rules = [</span><br><span class="line">    <span class="string">'device_id'</span>     =&gt; <span class="string">'string'</span>,</span><br><span class="line">    <span class="string">'username'</span>      =&gt; <span class="string">'required|min:3|max:25|alpha_num'</span>,</span><br><span class="line">    <span class="string">'email'</span>         =&gt; <span class="string">'required|email'</span>,</span><br><span class="line">    <span class="string">'password'</span>      =&gt; <span class="string">'required|min:6|max:20'</span>,</span><br><span class="line">    <span class="string">'invite_code'</span>   =&gt; <span class="string">'integer'</span></span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> ($res = Features::check(<span class="string">'signup.default'</span>)) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">$this</span>-&gt;json($res);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>校验json，例如：<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$rules = [</span><br><span class="line">    <span class="string">'batch'</span> =&gt; <span class="string">'required|json'</span>,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> ($error = <span class="keyword">$this</span>-&gt;validateInput($rules)) &#123;</span><br><span class="line">    <span class="keyword">return</span> $error;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="确认订单"><a href="#确认订单" class="headerlink" title="确认订单"></a>确认订单</h4><p>控制器方法</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>\<span class="title">v2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>\<span class="title">Controller</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Http</span>\<span class="title">Request</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>\<span class="title">Order</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>\<span class="title">Payment</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>\<span class="title">Features</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OrderController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * POST /ecapi.order.confirm</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">confirm</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $rules = [</span><br><span class="line">            <span class="string">'order'</span> =&gt; <span class="string">'required|integer|min:1'</span>,</span><br><span class="line">        ];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($error = <span class="keyword">$this</span>-&gt;validateInput($rules)) &#123;</span><br><span class="line">            <span class="keyword">return</span> $error;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $data = Order::confirm(<span class="keyword">$this</span>-&gt;validated);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">$this</span>-&gt;json($data);</span><br><span class="line">    &#125;</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>主要业务逻辑 Order::confirm():</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">BaseModel</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Helper</span>\<span class="title">Token</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">DB</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Services</span>\<span class="title">Shopex</span>\<span class="title">Erp</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">Log</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Helper</span>\<span class="title">Header</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Order</span> <span class="keyword">extends</span> <span class="title">BaseModel</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">protected</span> $connection = <span class="string">'shop'</span>;</span><br><span class="line">    <span class="keyword">protected</span> $table      = <span class="string">'order_info'</span>;</span><br><span class="line">    <span class="keyword">protected</span> $primaryKey = <span class="string">'order_id'</span>;</span><br><span class="line">    <span class="keyword">public</span> $timestamps = <span class="keyword">false</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">protected</span> $guarded = [];</span><br><span class="line">    <span class="keyword">protected</span> $appends = [<span class="string">'id'</span>, <span class="string">'sn'</span>, <span class="string">'total'</span>, <span class="string">'payment'</span>, <span class="string">'shipping'</span>, <span class="string">'invoice'</span>, <span class="string">'coupon'</span>, <span class="string">'score'</span>,<span class="string">'use_score'</span>, <span class="string">'cashgift'</span>, <span class="string">'consignee'</span>, <span class="string">'status'</span>, <span class="string">'created_at'</span>, <span class="string">'updated_at'</span>, <span class="string">'canceled_at'</span>, <span class="string">'paied_at'</span>, <span class="string">'shipping_at'</span>, <span class="string">'finish_at'</span>,<span class="string">'promos'</span>];</span><br><span class="line">    <span class="keyword">protected</span> $visible = [<span class="string">'id'</span>, <span class="string">'sn'</span>, <span class="string">'total'</span>,<span class="string">'tax'</span>, <span class="string">'goods'</span>, <span class="string">'payment'</span>, <span class="string">'shipping'</span>, <span class="string">'invoice'</span>, <span class="string">'coupon'</span>, <span class="string">'score'</span>, <span class="string">'use_score'</span>, <span class="string">'cashgift'</span>, <span class="string">'consignee'</span>, <span class="string">'status'</span>, <span class="string">'created_at'</span>, <span class="string">'updated_at'</span>, <span class="string">'canceled_at'</span>, <span class="string">'paied_at'</span>, <span class="string">'shipping_at'</span>, <span class="string">'finish_at'</span>, <span class="string">'discount_price'</span>, <span class="string">'promos'</span>, <span class="string">'extension_code'</span>];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// ECM 订单状态</span></span><br><span class="line">    <span class="keyword">const</span> STATUS_CREATED     = <span class="number">0</span>; <span class="comment">// 待付款</span></span><br><span class="line">    <span class="keyword">const</span> STATUS_PAID        = <span class="number">1</span>; <span class="comment">// 已付款</span></span><br><span class="line">    <span class="keyword">const</span> STATUS_DELIVERING  = <span class="number">2</span>; <span class="comment">// 发货中</span></span><br><span class="line">    <span class="keyword">const</span> STATUS_DELIVERIED  = <span class="number">3</span>; <span class="comment">// 已收货，待评价</span></span><br><span class="line">    <span class="keyword">const</span> STATUS_FINISHED    = <span class="number">4</span>; <span class="comment">// 已完成</span></span><br><span class="line">    <span class="keyword">const</span> STATUS_CANCELLED   = <span class="number">5</span>; <span class="comment">// 已取消</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 订单状态 */</span></span><br><span class="line">    <span class="keyword">const</span> OS_UNCONFIRMED     = <span class="number">0</span>; <span class="comment">// 未确认</span></span><br><span class="line">    <span class="keyword">const</span> OS_CONFIRMED       = <span class="number">1</span>; <span class="comment">// 已确认</span></span><br><span class="line">    <span class="keyword">const</span> OS_CANCELED        = <span class="number">2</span>; <span class="comment">// 已取消</span></span><br><span class="line">    <span class="keyword">const</span> OS_INVALID         = <span class="number">3</span>; <span class="comment">// 无效</span></span><br><span class="line">    <span class="keyword">const</span> OS_RETURNED        = <span class="number">4</span>; <span class="comment">// 退货</span></span><br><span class="line">    <span class="keyword">const</span> OS_SPLITED         = <span class="number">5</span>; <span class="comment">// 已分单</span></span><br><span class="line">    <span class="keyword">const</span> OS_SPLITING_PART   = <span class="number">6</span>; <span class="comment">// 部分分单</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 支付状态 */</span></span><br><span class="line">    <span class="keyword">const</span> PS_UNPAYED         = <span class="number">0</span>; <span class="comment">// 未付款</span></span><br><span class="line">    <span class="keyword">const</span> PS_PAYING          = <span class="number">1</span>; <span class="comment">// 付款中</span></span><br><span class="line">    <span class="keyword">const</span> PS_PAYED           = <span class="number">2</span>; <span class="comment">// 已付款</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/* 配送状态 */</span></span><br><span class="line">    <span class="keyword">const</span> SS_UNSHIPPED       = <span class="number">0</span>; <span class="comment">// 未发货</span></span><br><span class="line">    <span class="keyword">const</span> SS_SHIPPED         = <span class="number">1</span>; <span class="comment">// 已发货</span></span><br><span class="line">    <span class="keyword">const</span> SS_RECEIVED        = <span class="number">2</span>; <span class="comment">// 已收货</span></span><br><span class="line">    <span class="keyword">const</span> SS_PREPARING       = <span class="number">3</span>; <span class="comment">// 备货中</span></span><br><span class="line">    <span class="keyword">const</span> SS_SHIPPED_PART    = <span class="number">4</span>; <span class="comment">// 已发货(部分商品)</span></span><br><span class="line">    <span class="keyword">const</span> SS_SHIPPED_ING     = <span class="number">5</span>; <span class="comment">// 发货中(处理分单)</span></span><br><span class="line">    <span class="keyword">const</span> OS_SHIPPED_PART    = <span class="number">6</span>; <span class="comment">// 已发货(部分商品)</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> $reason    = [<span class="string">'改选其他商品'</span>, <span class="string">'改选其他配送方式'</span>, <span class="string">'不想买了'</span>, <span class="string">'其他原因'</span>];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">confirm</span><span class="params">(array $attributes)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        extract($attributes);</span><br><span class="line">        $uid = Token::authorization();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($model = <span class="keyword">self</span>::where([<span class="string">'user_id'</span> =&gt; $uid, <span class="string">'order_id'</span> =&gt; $order])-&gt;whereIn(<span class="string">'shipping_status'</span>, [<span class="keyword">self</span>::SS_SHIPPED, <span class="keyword">self</span>::SS_SHIPPED_PART, <span class="keyword">self</span>::OS_SHIPPED_PART])-&gt;first()) &#123;  <span class="comment">// 已收货 | 已发货(部分商品)</span></span><br><span class="line">            <span class="comment">//修改订单状态</span></span><br><span class="line">            $model-&gt;shipping_status = <span class="keyword">self</span>::SS_RECEIVED;  <span class="comment">// 已收货</span></span><br><span class="line">            $model-&gt;lastmodify = time();</span><br><span class="line">            <span class="keyword">if</span> ($model-&gt;save()) &#123;</span><br><span class="line">                <span class="comment">// 添加订单操作记录 </span></span><br><span class="line">                OrderAction::toCreateOrUpdate($model-&gt;order_id, $model-&gt;order_status, <span class="keyword">self</span>::SS_RECEIVED, $model-&gt;pay_status);</span><br><span class="line">                Erp::order($model-&gt;order_sn);</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">self</span>::formatBody([<span class="string">'order'</span> =&gt; $model-&gt;toArray()]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::formatError(<span class="keyword">self</span>::NOT_FOUND);</span><br><span class="line">    &#125;</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">    .</span><br></pre></td></tr></table></figure>
<p>用到 Erp 通知类：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span> </span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Services</span>\<span class="title">Shopex</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">Log</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>\<span class="title">ShopConfig</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Erp</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">order</span><span class="params">($order_sn, $type = <span class="string">'order_update'</span>)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $certificate_info = ShopConfig::findByCode(<span class="string">'certificate'</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span> (!$certificate_info) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $certificate = unserialize($certificate_info);</span><br><span class="line">        $token = <span class="string">""</span>;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">isset</span>($certificate[<span class="string">'token'</span>])) &#123;</span><br><span class="line">            $token = $certificate[<span class="string">'token'</span>];</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取物流信息参数</span></span><br><span class="line">        $param = <span class="keyword">array</span>(</span><br><span class="line">            <span class="string">'act'</span> =&gt; <span class="string">'ecmobile_fire_event'</span>,<span class="comment">//固定方法</span></span><br><span class="line">            <span class="string">'type'</span> =&gt; $type,  <span class="comment">// 比如 order_update</span></span><br><span class="line">            <span class="string">'id'</span> =&gt; $order_sn,<span class="comment">//订单号</span></span><br><span class="line">            <span class="string">'return_data'</span> =&gt; <span class="string">'json'</span>,<span class="comment">//返回类型</span></span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        $ac = <span class="keyword">self</span>::get_ac($param, $token);<span class="comment">//验证签名</span></span><br><span class="line"></span><br><span class="line">        $param[<span class="string">'ac'</span>] = $ac;<span class="comment">//签名值放入参数中</span></span><br><span class="line"></span><br><span class="line">        $api = config(<span class="string">'app.shop_url'</span>) . <span class="string">'/api.php'</span>;</span><br><span class="line"></span><br><span class="line">        $response = curl_request($api, <span class="string">'POST'</span>, $param);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($response[<span class="string">'result'</span>] == <span class="string">'success'</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        Log::error(<span class="string">'订单号：'</span> . $order_sn . <span class="string">' 同步ERP失败'</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//验证方法</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">get_ac</span><span class="params">($params, $token)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        ksort($params);</span><br><span class="line">        $tmp_verfy=<span class="string">''</span>;</span><br><span class="line">        <span class="keyword">foreach</span> ($params <span class="keyword">as</span> $key=&gt;$value) &#123;</span><br><span class="line">            $params[$key]=stripslashes($value);</span><br><span class="line">            $tmp_verfy.=$params[$key];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> strtolower(md5(trim($tmp_verfy.$token)));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="购物车结算"><a href="#购物车结算" class="headerlink" title="购物车结算"></a>购物车结算</h4><h5 id="生成订单号"><a href="#生成订单号" class="headerlink" title="生成订单号"></a>生成订单号</h5><p>代码见 <code>app\Models\v2\Cart.php</code> 的 <code>checkout</code> 方法。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取新订单号 验证订单号重复</span></span><br><span class="line"> <span class="keyword">do</span> &#123;</span><br><span class="line">     $order[<span class="string">'order_sn'</span>] = Order::get_order_sn();</span><br><span class="line"></span><br><span class="line">     $order_sn = Order::where(<span class="string">'order_sn'</span>, $order[<span class="string">'order_sn'</span>])-&gt;first();</span><br><span class="line"> &#125; <span class="keyword">while</span> (!<span class="keyword">empty</span>($order_sn));</span><br></pre></td></tr></table></figure>
<p>Order::get_order_sn():</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 得到新订单号</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span>  string</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">get_order_sn</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">/* 选择一个随机的方案 */</span></span><br><span class="line">    mt_srand((double) microtime() * <span class="number">1000000</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> date(<span class="string">'Ymd'</span>) . str_pad(mt_rand(<span class="number">1</span>, <span class="number">99999</span>), <span class="number">5</span>, <span class="string">'0'</span>, STR_PAD_LEFT);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="下单减库存"><a href="#下单减库存" class="headerlink" title="下单减库存"></a>下单减库存</h5><p>代码见 <code>app\Models\v2\Cart.php</code> 的 <code>checkout</code> 方法。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 如果使用库存，且下订单时减库存，则减少库存 */</span></span><br><span class="line"><span class="keyword">if</span> (ShopConfig::findByCode(<span class="string">'use_storage'</span>) == <span class="string">'1'</span> &amp;&amp; ShopConfig::findByCode(<span class="string">'stock_dec_time'</span>) == <span class="keyword">self</span>::SDT_PLACE) &#123;   <span class="comment">// self::SDT_PLACE = 1</span></span><br><span class="line">    Order::change_order_goods_storage($order[<span class="string">'order_id'</span>], <span class="keyword">true</span>, <span class="keyword">self</span>::SDT_PLACE);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>app\Models\v2\Order.php</code> 中的 <code>change_order_goods_storage()</code></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">BaseModel</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Helper</span>\<span class="title">Token</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">DB</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Services</span>\<span class="title">Shopex</span>\<span class="title">Erp</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">Log</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Helper</span>\<span class="title">Header</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Order</span> <span class="keyword">extends</span> <span class="title">BaseModel</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 改变订单中商品库存</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>   int     $order_id   订单号</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>   bool    $is_dec     是否减少库存</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>   bool    $storage     减库存的时机，1，下订单时；0，发货时；</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">change_order_goods_storage</span><span class="params">($order_id, $is_dec = true, $storage = <span class="number">0</span>)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">/* 查询订单商品信息 */</span></span><br><span class="line">        <span class="keyword">switch</span> ($storage) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">                <span class="comment">// $sql = "SELECT goods_id, SUM(send_number) AS num, MAX(extension_code) AS extension_code, product_id FROM " . $GLOBALS['ecs']-&gt;table('order_goods') .</span></span><br><span class="line">                <span class="comment">//         " WHERE order_id = '$order_id' AND is_real = 1 GROUP BY goods_id, product_id";</span></span><br><span class="line">                $res = OrderGoods::where(<span class="string">'order_id'</span>, $order_id)</span><br><span class="line">                        -&gt;groupBy(<span class="string">'goods_id'</span>)</span><br><span class="line">                        -&gt;groupBy(<span class="string">'product_id'</span>)</span><br><span class="line">                        -&gt;selectRaw(<span class="string">'sum(send_number) as num,goods_id,max(extension_code) as extension_code,product_id'</span>)</span><br><span class="line">                        -&gt;get();</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">                <span class="comment">// $sql = "SELECT goods_id, SUM(goods_number) AS num, MAX(extension_code) AS extension_code, product_id FROM " . $GLOBALS['ecs']-&gt;table('order_goods') .</span></span><br><span class="line">                <span class="comment">//         " WHERE order_id = '$order_id' AND is_real = 1 GROUP BY goods_id, product_id";</span></span><br><span class="line">                $res = OrderGoods::where(<span class="string">'order_id'</span>, $order_id)</span><br><span class="line">                        -&gt;groupBy(<span class="string">'goods_id'</span>)</span><br><span class="line">                        -&gt;groupBy(<span class="string">'product_id'</span>)</span><br><span class="line">                        -&gt;selectRaw(<span class="string">'sum(goods_number) as num,goods_id,max(extension_code) as extension_code,product_id'</span>)</span><br><span class="line">                        -&gt;get();</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">foreach</span> ($res <span class="keyword">as</span> $key =&gt; $row) &#123;</span><br><span class="line">            <span class="keyword">if</span> ($row[<span class="string">'extension_code'</span>] != <span class="string">"package_buy"</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> ($is_dec) &#123;</span><br><span class="line">                    <span class="keyword">self</span>::change_goods_storage($row[<span class="string">'goods_id'</span>], $row[<span class="string">'product_id'</span>], - $row[<span class="string">'num'</span>]);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">self</span>::change_goods_storage($row[<span class="string">'goods_id'</span>], $row[<span class="string">'product_id'</span>], $row[<span class="string">'num'</span>]);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// $GLOBALS['db']-&gt;query($sql);</span></span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;   <span class="comment">//package_buy</span></span><br><span class="line"></span><br><span class="line">                <span class="comment">// $sql = "SELECT goods_id, goods_number" .</span></span><br><span class="line">                <span class="comment">//        " FROM " . $GLOBALS['ecs']-&gt;table('package_goods') .</span></span><br><span class="line">                <span class="comment">//        " WHERE package_id = '" . $row['goods_id'] . "'";</span></span><br><span class="line">                <span class="comment">// $res_goods = $GLOBALS['db']-&gt;query($sql);</span></span><br><span class="line">                <span class="comment">// while ($row_goods = $GLOBALS['db']-&gt;fetchRow($res_goods))</span></span><br><span class="line">                <span class="comment">// &#123;</span></span><br><span class="line">                <span class="comment">//     $sql = "SELECT is_real" .</span></span><br><span class="line">                <span class="comment">//        " FROM " . $GLOBALS['ecs']-&gt;table('goods') .</span></span><br><span class="line">                <span class="comment">//        " WHERE goods_id = '" . $row_goods['goods_id'] . "'";</span></span><br><span class="line">                <span class="comment">//     $real_goods = $GLOBALS['db']-&gt;query($sql);</span></span><br><span class="line">                <span class="comment">//     $is_goods = $GLOBALS['db']-&gt;fetchRow($real_goods);</span></span><br><span class="line"></span><br><span class="line">                <span class="comment">//     if ($is_dec)</span></span><br><span class="line">                <span class="comment">//     &#123;</span></span><br><span class="line">                <span class="comment">//         self::change_goods_storage($row_goods['goods_id'], $row['product_id'], - ($row['num'] * $row_goods['goods_number']));</span></span><br><span class="line">                <span class="comment">//     &#125;</span></span><br><span class="line">                <span class="comment">//     elseif ($is_goods['is_real'])</span></span><br><span class="line">                <span class="comment">//     &#123;</span></span><br><span class="line">                <span class="comment">//         self::change_goods_storage($row_goods['goods_id'], $row['product_id'], ($row['num'] * $row_goods['goods_number']));</span></span><br><span class="line">                <span class="comment">//     &#125;</span></span><br><span class="line">                <span class="comment">// &#125;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">      <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 商品库存增与减 货品库存增与减</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>   int    $good_id         商品ID</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>   int    $product_id      货品ID</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span>   int    $number          增减数量，默认0；</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span>  bool               true，成功；false，失败；</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">change_goods_storage</span><span class="params">($good_id, $product_id, $number = <span class="number">0</span>)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($number == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>; <span class="comment">// 值为0即不做、增减操作，返回true</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">empty</span>($good_id) || <span class="keyword">empty</span>($number)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $number = ($number &gt; <span class="number">0</span>) ? <span class="string">'+ '</span> . $number : $number;</span><br><span class="line">        <span class="comment">/* 处理货品库存 */</span></span><br><span class="line">        $products_query = <span class="keyword">true</span>;</span><br><span class="line">        <span class="keyword">if</span> (!<span class="keyword">empty</span>($product_id)) &#123;</span><br><span class="line">            <span class="comment">// $sql = "UPDATE " . $GLOBALS['ecs']-&gt;table('products') ."</span></span><br><span class="line">            <span class="comment">//         SET product_number = product_number $number</span></span><br><span class="line">            <span class="comment">//         WHERE goods_id = '$good_id'</span></span><br><span class="line">            <span class="comment">//         AND product_id = '$product_id'</span></span><br><span class="line">            <span class="comment">//         LIMIT 1";</span></span><br><span class="line">            <span class="comment">// $products_query = $GLOBALS['db']-&gt;query($sql);</span></span><br><span class="line">            $products_query = Products::where(<span class="string">'goods_id'</span>, $good_id)</span><br><span class="line">                                    -&gt;where(<span class="string">'product_id'</span>, $product_id)</span><br><span class="line">                                    -&gt;limit(<span class="number">1</span>)</span><br><span class="line">                                    -&gt;increment(<span class="string">'product_number'</span>, $number);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/* 处理商品库存 */</span></span><br><span class="line">        <span class="comment">// $sql = "UPDATE " . $GLOBALS['ecs']-&gt;table('goods') ."</span></span><br><span class="line">        <span class="comment">//         SET goods_number = goods_number $number</span></span><br><span class="line">        <span class="comment">//         WHERE goods_id = '$good_id'</span></span><br><span class="line">        <span class="comment">//         LIMIT 1";</span></span><br><span class="line">        <span class="comment">// $query = $GLOBALS['db']-&gt;query($sql);</span></span><br><span class="line">        $query = Goods::where(<span class="string">'goods_id'</span>, $good_id)</span><br><span class="line">                      -&gt;limit(<span class="number">1</span>)</span><br><span class="line">                      -&gt;increment(<span class="string">'goods_number'</span>, $number);</span><br><span class="line">        <span class="keyword">if</span> ($query &amp;&amp; $products_query) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="版本检查接口"><a href="#版本检查接口" class="headerlink" title="版本检查接口"></a>版本检查接口</h4><p>控制器</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>\<span class="title">v2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Http</span>\<span class="title">Controllers</span>\<span class="title">Controller</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Http</span>\<span class="title">Request</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>\<span class="title">Version</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">VersionController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * POST ecapi.version.check</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">check</span><span class="params">(Request $request)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $data = Version::checkVersion();</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">$this</span>-&gt;json($data);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>版本检查逻辑 <code>Version::checkVersion()</code></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">v2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Models</span>\<span class="title">BaseModel</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">Helper</span>\<span class="title">Header</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Version</span> <span class="keyword">extends</span> <span class="title">BaseModel</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">protected</span> $connection = <span class="string">'shop'</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">protected</span> $table      = <span class="string">'version'</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> $timestamps   = <span class="keyword">true</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">protected</span> $appends = [<span class="string">'download_url'</span>];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">protected</span> $visible = [<span class="string">'version'</span>, <span class="string">'download_url'</span>, <span class="string">'content'</span>];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">checkVersion</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $arr = Header::getUserAgent();</span><br><span class="line">        $ver = Header::getVer();</span><br><span class="line"></span><br><span class="line">        $platform = Header::getUserAgent(<span class="string">'Platform'</span>);</span><br><span class="line">        <span class="keyword">switch</span> ($platform) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'ios'</span>:</span><br><span class="line">                $platform = <span class="number">1</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'android'</span>:</span><br><span class="line">                $platform = <span class="number">2</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                $platform = <span class="number">0</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (is_array($arr) &amp;&amp; <span class="keyword">isset</span>($arr[<span class="string">'Platform'</span>]) &amp;&amp; !<span class="keyword">empty</span>($ver)) &#123;</span><br><span class="line">            $model = Version::where(<span class="string">'platform'</span>, $platform)-&gt;orderBy(<span class="string">'version'</span>, <span class="string">'DESC'</span>)-&gt;first();</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">isset</span>($model-&gt;version) &amp;&amp; version_compare($ver, $model-&gt;version) &lt; <span class="number">0</span>) &#123; <span class="comment">// 需要更新版本</span></span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">self</span>::formatBody([<span class="string">'version_info'</span> =&gt; $model]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">self</span>::formatBody([<span class="string">'version_info'</span> =&gt; <span class="keyword">null</span>]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getDownloadUrlAttribute</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">$this</span>-&gt;attributes[<span class="string">'url'</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>获取头部信息的Header类：<br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">App</span>\<span class="title">Helper</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Header</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getUserAgent</span><span class="params">($key = false)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $arr = [];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($ua = app(<span class="string">'request'</span>)-&gt;header(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-UserAgent'</span>)) &#123;</span><br><span class="line">            $items = @explode(<span class="string">', '</span>, $ua);</span><br><span class="line">            <span class="keyword">if</span> (is_array($items)) &#123;</span><br><span class="line">                <span class="keyword">foreach</span> ($items <span class="keyword">as</span> $property) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (strpos($property, <span class="string">'/'</span>) !== <span class="keyword">false</span>) &#123;</span><br><span class="line">                        $property = explode(<span class="string">'/'</span>, $property);</span><br><span class="line">                        $arr[$property[<span class="number">0</span>]] = $property[<span class="number">1</span>];</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($key) &#123;</span><br><span class="line">            <span class="keyword">return</span> (<span class="keyword">isset</span>($arr[$key]) &amp;&amp; $arr[$key]) ? strtolower($arr[$key]) : <span class="string">''</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> $arr;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getVer</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($ver = app(<span class="string">'request'</span>)-&gt;header(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-Ver'</span>)) &#123;</span><br><span class="line">            $rule = <span class="string">'/^[(\d)+.(\d)+.(\d)+]+$/'</span>;</span><br><span class="line">            <span class="keyword">if</span> (preg_match($rule, $ver)) &#123;</span><br><span class="line">                <span class="keyword">return</span> $ver;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getUDID</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($UDID = app(<span class="string">'request'</span>)-&gt;header(<span class="string">'X-'</span>.config(<span class="string">'app.name'</span>).<span class="string">'-UDID'</span>)) &#123;</span><br><span class="line">            <span class="keyword">return</span> $UDID;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="助手函数"><a href="#助手函数" class="headerlink" title="助手函数"></a>助手函数</h4><p>见 <code>app/Helper/common.php</code></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (! function_exists(<span class="string">'curl_request'</span>)) &#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * CURL Request</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">curl_request</span><span class="params">($api, $method = <span class="string">'GET'</span>, $params = array<span class="params">()</span>, $headers = [], $json_decode = true)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $curl = curl_init();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">switch</span> (strtoupper($method)) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'GET'</span>:</span><br><span class="line">                <span class="keyword">if</span> (!<span class="keyword">empty</span>($params)) &#123;</span><br><span class="line">                    $api .= (strpos($api, <span class="string">'?'</span>) ? <span class="string">'&amp;'</span> : <span class="string">'?'</span>) . http_build_query($params);</span><br><span class="line">                &#125;</span><br><span class="line">                curl_setopt($curl, CURLOPT_HTTPGET, <span class="keyword">true</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'POST'</span>:</span><br><span class="line">                curl_setopt($curl, CURLOPT_POST, <span class="keyword">true</span>);</span><br><span class="line">                curl_setopt($curl, CURLOPT_POSTFIELDS, $params);</span><br><span class="line"></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'PUT'</span>:</span><br><span class="line">                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, <span class="string">'PUT'</span>);</span><br><span class="line">                curl_setopt($curl, CURLOPT_POSTFIELDS, $params);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'DELETE'</span>:</span><br><span class="line">                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, <span class="string">'DELETE'</span>);</span><br><span class="line">                curl_setopt($curl, CURLOPT_POSTFIELDS, $params);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        curl_setopt($curl, CURLOPT_URL, $api);</span><br><span class="line">        curl_setopt($curl, CURLOPT_RETURNTRANSFER, <span class="keyword">true</span>);</span><br><span class="line">        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, <span class="keyword">false</span>);</span><br><span class="line">        curl_setopt($curl, CURLOPT_HEADER, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);</span><br><span class="line"></span><br><span class="line">        $response = curl_exec($curl);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($response === <span class="keyword">false</span>) &#123;</span><br><span class="line">            $error = curl_error($curl);</span><br><span class="line">            \Log::debug(<span class="string">'curl请求失败'</span> . json_encode($error));</span><br><span class="line">            curl_close($curl);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 解决windows 服务器 BOM 问题</span></span><br><span class="line">            $response = trim($response, chr(<span class="number">239</span>).chr(<span class="number">187</span>).chr(<span class="number">191</span>));</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> ($json_decode) &#123;</span><br><span class="line">                $response = json_decode($response, <span class="keyword">true</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        curl_close($curl);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $response;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="后台"><a href="#后台" class="headerlink" title="后台"></a>后台</h3><h4 id="数据库设计"><a href="#数据库设计" class="headerlink" title="数据库设计"></a>数据库设计</h4><p>下面只是写了下商品模块相关的设计。</p>
<p>商品表 <code>ecs_goods</code></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`ecs_goods`</span> (</span><br><span class="line">  <span class="string">`goods_id`</span> mediumint(<span class="number">8</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`cat_id`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`goods_sn`</span> <span class="built_in">varchar</span>(<span class="number">60</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`goods_name`</span> <span class="built_in">varchar</span>(<span class="number">120</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`goods_name_style`</span> <span class="built_in">varchar</span>(<span class="number">60</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'+'</span>,</span><br><span class="line">  <span class="string">`click_count`</span> <span class="built_in">int</span>(<span class="number">10</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`brand_id`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`provider_name`</span> <span class="built_in">varchar</span>(<span class="number">100</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`goods_number`</span> mediumint(<span class="number">8</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`goods_weight`</span> <span class="built_in">decimal</span>(<span class="number">10</span>,<span class="number">3</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0.000'</span>,</span><br><span class="line">  <span class="string">`market_price`</span> <span class="built_in">decimal</span>(<span class="number">10</span>,<span class="number">2</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0.00'</span>,</span><br><span class="line">  <span class="string">`virtual_sales`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`shop_price`</span> <span class="built_in">decimal</span>(<span class="number">10</span>,<span class="number">2</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0.00'</span>,</span><br><span class="line">  <span class="string">`promote_price`</span> <span class="built_in">decimal</span>(<span class="number">10</span>,<span class="number">2</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0.00'</span>,</span><br><span class="line">  <span class="string">`promote_start_date`</span> <span class="built_in">int</span>(<span class="number">11</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`promote_end_date`</span> <span class="built_in">int</span>(<span class="number">11</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`warn_number`</span> tinyint(<span class="number">3</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'1'</span>,</span><br><span class="line">  <span class="string">`keywords`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`goods_brief`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`goods_desc`</span> <span class="built_in">text</span> <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line">  <span class="string">`goods_thumb`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`goods_img`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`original_img`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`is_real`</span> tinyint(<span class="number">3</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'1'</span>,</span><br><span class="line">  <span class="string">`extension_code`</span> <span class="built_in">varchar</span>(<span class="number">30</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`is_on_sale`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'1'</span>,</span><br><span class="line">  <span class="string">`is_alone_sale`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'1'</span>,</span><br><span class="line">  <span class="string">`is_shipping`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`integral`</span> <span class="built_in">int</span>(<span class="number">10</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`add_time`</span> <span class="built_in">int</span>(<span class="number">10</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`sort_order`</span> <span class="built_in">smallint</span>(<span class="number">4</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'100'</span>,</span><br><span class="line">  <span class="string">`is_delete`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`is_best`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`is_new`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`is_hot`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`is_promote`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`bonus_type_id`</span> tinyint(<span class="number">3</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`last_update`</span> <span class="built_in">int</span>(<span class="number">10</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`goods_type`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`seller_note`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`give_integral`</span> <span class="built_in">int</span>(<span class="number">11</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'-1'</span>,</span><br><span class="line">  <span class="string">`rank_integral`</span> <span class="built_in">int</span>(<span class="number">11</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'-1'</span>,</span><br><span class="line">  <span class="string">`suppliers_id`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">DEFAULT</span> <span class="literal">NULL</span>,</span><br><span class="line">  <span class="string">`is_check`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">DEFAULT</span> <span class="literal">NULL</span>,</span><br><span class="line">  PRIMARY <span class="keyword">KEY</span> (<span class="string">`goods_id`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`goods_sn`</span> (<span class="string">`goods_sn`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`cat_id`</span> (<span class="string">`cat_id`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`last_update`</span> (<span class="string">`last_update`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`brand_id`</span> (<span class="string">`brand_id`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`goods_weight`</span> (<span class="string">`goods_weight`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`promote_end_date`</span> (<span class="string">`promote_end_date`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`promote_start_date`</span> (<span class="string">`promote_start_date`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`goods_number`</span> (<span class="string">`goods_number`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`sort_order`</span> (<span class="string">`sort_order`</span>)</span><br><span class="line">) <span class="keyword">ENGINE</span>=MyISAM AUTO_INCREMENT=<span class="number">73</span> <span class="keyword">DEFAULT</span> <span class="keyword">CHARSET</span>=utf8;</span><br></pre></td></tr></table></figure>
<p>商品属性表：<code>ecs_goods_attr</code></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`ecs_goods_attr`</span> (</span><br><span class="line">  <span class="string">`goods_attr_id`</span> <span class="built_in">int</span>(<span class="number">10</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`goods_id`</span> mediumint(<span class="number">8</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`attr_id`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`attr_value`</span> <span class="built_in">text</span> <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line">  <span class="string">`attr_price`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  PRIMARY <span class="keyword">KEY</span> (<span class="string">`goods_attr_id`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`goods_id`</span> (<span class="string">`goods_id`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`attr_id`</span> (<span class="string">`attr_id`</span>)</span><br><span class="line">) <span class="keyword">ENGINE</span>=MyISAM AUTO_INCREMENT=<span class="number">239</span> <span class="keyword">DEFAULT</span> <span class="keyword">CHARSET</span>=utf8;</span><br></pre></td></tr></table></figure>
<p>商品分类 <code>ecs_category</code></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`ecs_category`</span> (</span><br><span class="line">  <span class="string">`cat_id`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`cat_name`</span> <span class="built_in">varchar</span>(<span class="number">90</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`keywords`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`cat_desc`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`parent_id`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`sort_order`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'50'</span> <span class="keyword">COMMENT</span> <span class="string">'小的排前面'</span>,</span><br><span class="line">  <span class="string">`template_file`</span> <span class="built_in">varchar</span>(<span class="number">50</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`measure_unit`</span> <span class="built_in">varchar</span>(<span class="number">15</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span> <span class="keyword">COMMENT</span> <span class="string">'数量单位'</span>,</span><br><span class="line">  <span class="string">`show_in_nav`</span> tinyint(<span class="number">1</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`style`</span> <span class="built_in">varchar</span>(<span class="number">150</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line">  <span class="string">`is_show`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'1'</span>,</span><br><span class="line">  <span class="string">`grade`</span> tinyint(<span class="number">4</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`filter_attr`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  PRIMARY <span class="keyword">KEY</span> (<span class="string">`cat_id`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`parent_id`</span> (<span class="string">`parent_id`</span>)</span><br><span class="line">) <span class="keyword">ENGINE</span>=MyISAM AUTO_INCREMENT=<span class="number">33</span> <span class="keyword">DEFAULT</span> <span class="keyword">CHARSET</span>=utf8;</span><br></pre></td></tr></table></figure>
<p>商品品牌<code>ecs_brand</code><br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`ecs_brand`</span> (</span><br><span class="line">  <span class="string">`brand_id`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`brand_name`</span> <span class="built_in">varchar</span>(<span class="number">60</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`brand_logo`</span> <span class="built_in">varchar</span>(<span class="number">80</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`brand_desc`</span> <span class="built_in">text</span> <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line">  <span class="string">`site_url`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`sort_order`</span> tinyint(<span class="number">3</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'50'</span>,</span><br><span class="line">  <span class="string">`is_show`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'1'</span>,</span><br><span class="line">  PRIMARY <span class="keyword">KEY</span> (<span class="string">`brand_id`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`is_show`</span> (<span class="string">`is_show`</span>)</span><br><span class="line">) <span class="keyword">ENGINE</span>=MyISAM AUTO_INCREMENT=<span class="number">16</span> <span class="keyword">DEFAULT</span> <span class="keyword">CHARSET</span>=utf8;</span><br></pre></td></tr></table></figure></p>
<p>商品相册<code>ecs_goods_gallery</code></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`ecs_goods_gallery`</span> (</span><br><span class="line">  <span class="string">`img_id`</span> mediumint(<span class="number">8</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`goods_id`</span> mediumint(<span class="number">8</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`img_url`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`img_desc`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`thumb_url`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`img_original`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`sort_order`</span> <span class="built_in">smallint</span>(<span class="number">4</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'30'</span>,</span><br><span class="line">  PRIMARY <span class="keyword">KEY</span> (<span class="string">`img_id`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`goods_id`</span> (<span class="string">`goods_id`</span>)</span><br><span class="line">) <span class="keyword">ENGINE</span>=MyISAM AUTO_INCREMENT=<span class="number">44</span> <span class="keyword">DEFAULT</span> <span class="keyword">CHARSET</span>=utf8;</span><br></pre></td></tr></table></figure>
<p>商品属性类型  <code>ecs_goods_type</code></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`ecs_goods_type`</span> (</span><br><span class="line">  <span class="string">`cat_id`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`cat_name`</span> <span class="built_in">varchar</span>(<span class="number">60</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span>,</span><br><span class="line">  <span class="string">`enabled`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'1'</span>,</span><br><span class="line">  <span class="string">`attr_group`</span> <span class="built_in">varchar</span>(<span class="number">255</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span>,</span><br><span class="line">  PRIMARY <span class="keyword">KEY</span> (<span class="string">`cat_id`</span>)</span><br><span class="line">) <span class="keyword">ENGINE</span>=MyISAM AUTO_INCREMENT=<span class="number">10</span> <span class="keyword">DEFAULT</span> <span class="keyword">CHARSET</span>=utf8;</span><br></pre></td></tr></table></figure>
<p>商品属性 <code>ecs_attribute</code><br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="string">`ecs_attribute`</span> (</span><br><span class="line">  <span class="string">`attr_id`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  <span class="string">`cat_id`</span> <span class="built_in">smallint</span>(<span class="number">5</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span> <span class="keyword">COMMENT</span> <span class="string">'对应ecs_goods_type的cat_id'</span>,</span><br><span class="line">  <span class="string">`attr_name`</span> <span class="built_in">varchar</span>(<span class="number">60</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">''</span> <span class="keyword">COMMENT</span> <span class="string">'属性名称'</span>,</span><br><span class="line">  <span class="string">`attr_input_type`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'1'</span> <span class="keyword">COMMENT</span> <span class="string">'该属性值的录入方式 0手动输入 1:从列表中(attr_values)选择'</span>,</span><br><span class="line">  <span class="string">`attr_type`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'1'</span> <span class="keyword">COMMENT</span> <span class="string">'属性是否可选 0唯一属性 1单选属性 2多值属性, 为0时为属性，不为0位规格'</span>,</span><br><span class="line">  <span class="string">`attr_values`</span> <span class="built_in">text</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">COMMENT</span> <span class="string">'可选值方式'</span>,</span><br><span class="line">  <span class="string">`attr_index`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span> <span class="keyword">COMMENT</span> <span class="string">'检索方式 0-&gt;唯一；1-&gt;单选；2-&gt;多选'</span>,</span><br><span class="line">  <span class="string">`sort_order`</span> tinyint(<span class="number">3</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  <span class="string">`is_linked`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span> <span class="keyword">COMMENT</span> <span class="string">'相同属性值的商品是否关联'</span>,</span><br><span class="line">  <span class="string">`attr_group`</span> tinyint(<span class="number">1</span>) <span class="keyword">unsigned</span> <span class="keyword">NOT</span> <span class="literal">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">'0'</span>,</span><br><span class="line">  PRIMARY <span class="keyword">KEY</span> (<span class="string">`attr_id`</span>),</span><br><span class="line">  <span class="keyword">KEY</span> <span class="string">`cat_id`</span> (<span class="string">`cat_id`</span>)</span><br><span class="line">) <span class="keyword">ENGINE</span>=MyISAM AUTO_INCREMENT=<span class="number">211</span> <span class="keyword">DEFAULT</span> <span class="keyword">CHARSET</span>=utf8;</span><br></pre></td></tr></table></figure></p>
<h4 id="报表统计"><a href="#报表统计" class="headerlink" title="报表统计"></a>报表统计</h4><p>都是做的实时统计</p>
<p>例订单统计，代码见<code>admin/order_stats.php</code>:</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 计算订单各种费用之和的语句 */</span></span><br><span class="line">  $total_fee = <span class="string">" SUM("</span> . order_amount_field() . <span class="string">") AS total_turnover "</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/* 取得订单转化率数据 */</span></span><br><span class="line">  $sql = <span class="string">"SELECT COUNT(*) AS total_order_num, "</span> .$total_fee.</span><br><span class="line">         <span class="string">" FROM "</span> . $ecs-&gt;table(<span class="string">'order_info'</span>).</span><br><span class="line">         <span class="string">" WHERE 1 "</span> . order_query_sql(<span class="string">'finished'</span>);</span><br><span class="line">  $order_general = $db-&gt;getRow($sql);</span><br><span class="line">  $order_general[<span class="string">'total_turnover'</span>] = floatval($order_general[<span class="string">'total_turnover'</span>]);</span><br><span class="line"></span><br><span class="line">  <span class="comment">/* 取得商品总点击数量 */</span></span><br><span class="line">  $sql = <span class="string">'SELECT SUM(click_count) FROM '</span> .$ecs-&gt;table(<span class="string">'goods'</span>) .<span class="string">' WHERE is_delete = 0'</span>;</span><br><span class="line">  $click_count = floatval($db-&gt;getOne($sql));</span><br><span class="line"></span><br><span class="line">  <span class="comment">/* 每千个点击的订单数 */</span></span><br><span class="line">  $click_ordernum = $click_count &gt; <span class="number">0</span> ? round(($order_general[<span class="string">'total_order_num'</span>] * <span class="number">1000</span>)/$click_count,<span class="number">2</span>) : <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/* 每千个点击的购物额 */</span></span><br><span class="line">  $click_turnover = $click_count &gt; <span class="number">0</span> ? round(($order_general[<span class="string">'total_turnover'</span>] * <span class="number">1000</span>)/$click_count,<span class="number">2</span>) : <span class="number">0</span>;</span><br></pre></td></tr></table></figure>
<p>生成查询语句的<code>order_query_sql()</code>，代码见 <code>include/lib_order.php</code><br><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 生成查询订单的sql</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span>   string  $type   类型</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span>   string  $alias  order表的别名（包括.例如 o.）</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span>  string</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">order_query_sql</span><span class="params">($type = <span class="string">'finished'</span>, $alias = <span class="string">''</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">/* 已完成订单 */</span></span><br><span class="line">    <span class="keyword">if</span> ($type == <span class="string">'finished'</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">" AND &#123;$alias&#125;order_status "</span> . db_create_in(<span class="keyword">array</span>(OS_CONFIRMED, OS_SPLITED)) .</span><br><span class="line">               <span class="string">" AND &#123;$alias&#125;shipping_status "</span> . db_create_in(<span class="keyword">array</span>(SS_SHIPPED, SS_RECEIVED)) .</span><br><span class="line">               <span class="string">" AND &#123;$alias&#125;pay_status "</span> . db_create_in(<span class="keyword">array</span>(PS_PAYED, PS_PAYING)) . <span class="string">" "</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 待发货订单 */</span></span><br><span class="line">    <span class="keyword">elseif</span> ($type == <span class="string">'await_ship'</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">" AND   &#123;$alias&#125;order_status "</span> .</span><br><span class="line">                 db_create_in(<span class="keyword">array</span>(OS_CONFIRMED, OS_SPLITED, OS_SPLITING_PART)) .</span><br><span class="line">               <span class="string">" AND   &#123;$alias&#125;shipping_status "</span> .</span><br><span class="line">                 db_create_in(<span class="keyword">array</span>(SS_UNSHIPPED, SS_PREPARING, SS_SHIPPED_ING)) .</span><br><span class="line">               <span class="string">" AND ( &#123;$alias&#125;pay_status "</span> . db_create_in(<span class="keyword">array</span>(PS_PAYED, PS_PAYING)) . <span class="string">" OR &#123;$alias&#125;pay_id "</span> . db_create_in(payment_id_list(<span class="keyword">true</span>)) . <span class="string">") "</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 待付款订单 */</span></span><br><span class="line">    <span class="keyword">elseif</span> ($type == <span class="string">'await_pay'</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">" AND   &#123;$alias&#125;order_status "</span> . db_create_in(<span class="keyword">array</span>(OS_CONFIRMED, OS_SPLITED)) .</span><br><span class="line">               <span class="string">" AND   &#123;$alias&#125;pay_status = '"</span> . PS_UNPAYED . <span class="string">"'"</span> .</span><br><span class="line">               <span class="string">" AND ( &#123;$alias&#125;shipping_status "</span> . db_create_in(<span class="keyword">array</span>(SS_SHIPPED, SS_RECEIVED)) . <span class="string">" OR &#123;$alias&#125;pay_id "</span> . db_create_in(payment_id_list(<span class="keyword">false</span>)) . <span class="string">") "</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 未确认订单 */</span></span><br><span class="line">    <span class="keyword">elseif</span> ($type == <span class="string">'unconfirmed'</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">" AND &#123;$alias&#125;order_status = '"</span> . OS_UNCONFIRMED . <span class="string">"' "</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 未处理订单：用户可操作 */</span></span><br><span class="line">    <span class="keyword">elseif</span> ($type == <span class="string">'unprocessed'</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">" AND &#123;$alias&#125;order_status "</span> . db_create_in(<span class="keyword">array</span>(OS_UNCONFIRMED, OS_CONFIRMED)) .</span><br><span class="line">               <span class="string">" AND &#123;$alias&#125;shipping_status = '"</span> . SS_UNSHIPPED . <span class="string">"'"</span> .</span><br><span class="line">               <span class="string">" AND &#123;$alias&#125;pay_status = '"</span> . PS_UNPAYED . <span class="string">"' "</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 未付款未发货订单：管理员可操作 */</span></span><br><span class="line">    <span class="keyword">elseif</span> ($type == <span class="string">'unpay_unship'</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">" AND &#123;$alias&#125;order_status "</span> . db_create_in(<span class="keyword">array</span>(OS_UNCONFIRMED, OS_CONFIRMED)) .</span><br><span class="line">               <span class="string">" AND &#123;$alias&#125;shipping_status "</span> . db_create_in(<span class="keyword">array</span>(SS_UNSHIPPED, SS_PREPARING)) .</span><br><span class="line">               <span class="string">" AND &#123;$alias&#125;pay_status = '"</span> . PS_UNPAYED . <span class="string">"' "</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 已发货订单：不论是否付款 */</span></span><br><span class="line">    <span class="keyword">elseif</span> ($type == <span class="string">'shipped'</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">" AND &#123;$alias&#125;order_status = '"</span> . OS_CONFIRMED . <span class="string">"'"</span> .</span><br><span class="line">               <span class="string">" AND &#123;$alias&#125;shipping_status "</span> . db_create_in(<span class="keyword">array</span>(SS_SHIPPED, SS_RECEIVED)) . <span class="string">" "</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">die</span>(<span class="string">'函数 order_query_sql 参数错误'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>

      
    </div>

    

    
    
    
<div>
  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">------本文结束<i class="fa fa-paw"></i>感谢阅读------</div>
    
</div>
  
</div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>欣赏此文？求鼓励，求支持！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat.png" alt="云舒 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.png" alt="云舒 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>云舒</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://blog.yunshu168.com/posts/b727584f.html" title="ECShop开源系统代码学习笔记">http://blog.yunshu168.com/posts/b727584f.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明出处！</li>
</ul>

      </div>
    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/74af3221.html" rel="next" title="从零开始搭建创业公司技术架构">
                <i class="fa fa-chevron-left"></i> 从零开始搭建创业公司技术架构
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/287bba86.html" rel="prev" title="从零开始学架构">
                从零开始学架构 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        
  <div class="bdsharebuttonbox">
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
    <a href="#" class="bds_douban" data-cmd="douban" title="分享到豆瓣网"></a>
    <a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a>
    <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间"></a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a>
    <a href="#" class="bds_tieba" data-cmd="tieba" title="分享到百度贴吧"></a>
    <a href="#" class="bds_twi" data-cmd="twi" title="分享到Twitter"></a>
    <a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook"></a>
    <a href="#" class="bds_more" data-cmd="more"></a>
    <a class="bds_count" data-cmd="count"></a>
  </div>
  <script>
    window._bd_share_config = {
      "common": {
        "bdText": "",
        "bdMini": "2",
        "bdMiniList": false,
        "bdPic": ""
      },
      "share": {
        "bdSize": "16",
        "bdStyle": "0"
      },
      "image": {
        "viewList": ["tsina", "douban", "sqq", "qzone", "weixin", "twi", "fbook"],
        "viewText": "分享到：",
        "viewSize": "16"
      }
    }
  </script>

<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
    </div>
  </div>


          </div>
          

  
    <div class="comments" id="comments">
      
        <div id="gitment-container"></div>
      
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpg"
                alt="云舒" />
            
              <p class="site-author-name" itemprop="name">云舒</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives">
                
                    <span class="site-state-item-count">68</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">5</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">28</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/yunshu2009" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:yunshu.luo@gmail.com" target="_blank" title="E-Mail"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.cnblogs.com/luoyunshu/" target="_blank" title="Cnblogs"><i class="fa fa-fw fa-globe"></i>Cnblogs</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.bilibili.com" target="_blank" title="bilibili"><i class="fa fa-fw fa-globe"></i>bilibili</a>
                  
                </span>
              
            </div>
          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#接口设计"><span class="nav-number">1.</span> <span class="nav-text">接口设计</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#登录接口"><span class="nav-number">1.1.</span> <span class="nav-text">登录接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#需要认证的接口"><span class="nav-number">1.2.</span> <span class="nav-text">需要认证的接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#匿名访问接口"><span class="nav-number">1.3.</span> <span class="nav-text">匿名访问接口</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#接口实现"><span class="nav-number">2.</span> <span class="nav-text">接口实现</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Model-基类"><span class="nav-number">2.1.</span> <span class="nav-text">Model 基类</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数校验和返回json结果"><span class="nav-number">2.2.</span> <span class="nav-text">参数校验和返回json结果</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#功能开启检查"><span class="nav-number">2.3.</span> <span class="nav-text">功能开启检查</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数校验规则"><span class="nav-number">2.4.</span> <span class="nav-text">参数校验规则</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#确认订单"><span class="nav-number">2.5.</span> <span class="nav-text">确认订单</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#购物车结算"><span class="nav-number">2.6.</span> <span class="nav-text">购物车结算</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#生成订单号"><span class="nav-number">2.6.1.</span> <span class="nav-text">生成订单号</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#下单减库存"><span class="nav-number">2.6.2.</span> <span class="nav-text">下单减库存</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#版本检查接口"><span class="nav-number">2.7.</span> <span class="nav-text">版本检查接口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#助手函数"><span class="nav-number">2.8.</span> <span class="nav-text">助手函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#后台"><span class="nav-number">3.</span> <span class="nav-text">后台</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#数据库设计"><span class="nav-number">3.1.</span> <span class="nav-text">数据库设计</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#报表统计"><span class="nav-number">3.2.</span> <span class="nav-text">报表统计</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2012 &mdash; <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">云舒</span>

  

  
</div>




  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/theme-next/hexo-theme-next">NexT.Pisces</a></div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.0.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.0.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.0.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.0.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.0.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.0.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.0.4"></script>



  



	





  





  






<!-- LOCAL: You can save these files to your site and update links -->
    
        
         <!-- <link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css"> -->
        <!-- <script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script> -->
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/theme-next/theme-next-gitment@1/default.css">
        <script src="https://cdn.jsdelivr.net/gh/theme-next/theme-next-gitment@1/gitment.browser.js"></script>
    
<!-- END LOCAL -->

    

    
      <script type="text/javascript">
      function renderGitment(){
        var gitment = new Gitment({
            id: decodeURI(window.location.pathname),
            owner: 'yunshu2009',
            repo: 'yunshu2009.github.io',
            
            oauth: {
            
            
                client_secret: '15cdd9dc30fb9e0c97260bf79fe09d158fa04b22',
            
                client_id: '9495da16cda475a5e221'
            }});
        gitment.render('gitment-container');
      }

      
      renderGitment();
      
      </script>
    






  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  

  
  

  

  

  

  


</body>
</html>
